<?php
class AccountTokenSearch extends CFormModel {
	public $tokenYear;
	public $tokenMonth;
	public $numberMin;
	public $numberMax;
	public $printResult;
	public $singlePage;
	public $categoryId;
	public $summary;

	public function rules() {
		return array(
			array(
				'singlePage, numberMin, numberMax, tokenYear, tokenMonth, printResult, categoryId',
				'numerical',
				'integerOnly' => true,
			),
			array(
				'summary',
				'length',
				'max' => 255
			),
		);
	}

	public function attributeLabels() {
		return array(
			'numberMin' => '编号',
			'tokenYear' => '时间',
			'singlePage' => '显示全部',
			'printResult' => '打印凭证',
			'categoryId' => '包含科目',
			'summary' => '包含摘要'
		);
	}

	/**
	 * @return CDbCriteria
	 */
	public function createCriteria() {
		$criteria = new CDbCriteria;

		if ($this->numberMax) {
			$criteria->compare('number', '<=' . $this->numberMax);
		}

		if ($this->numberMin) {
			$criteria->compare('number', '>=' . $this->numberMin);
		}

		$criteria->compare('tokenYear', $this->tokenYear);
		$criteria->compare('tokenMonth', $this->tokenMonth);

		if ($projectId = AccountTokenProject::getGlobalProjectId()) {
			$criteria->compare('projectId', $projectId);
		} else {
			$criteria->addInCondition('projectId', AccountTokenProject::getCompanyProjectIds());
		}
		$criteria->with = array(
			'createUser.employee'
		);
		if ($this->categoryId || $this->summary) {
			$criteria->with[] = 'items';
			$criteria->together = true;
			$criteria->addSearchCondition('summary', $this->summary);
			$criteria->compare('categoryId', $this->categoryId);
		}

		$criteria->order = 'tokenYear DESC, tokenMonth DESC, number DESC';

		return $criteria;
	}
}
